<?php

namespace app\api\controller;

use app\admin\model\Order;
use app\admin\model\Orderitem;

use think\Db;
use think\Exception;

class MessageLogistics extends Base
{

    /**
     * 交易物流消息列表
     */
    public function getList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->memberId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '会员ID(memberId) 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'member_id' => $param->memberId
        ];

        $model = db('message_logistics');
        $fields = '*';
        $order = 'create_date DESC';

        $list = $model->where($where)->group('express_no')->field($fields)->limit($start, 100)->order($order)->select();
        if (!$list) {
            return show(config('status.ERROR_STATUS'), '没有消息', '没有消息');
        }
        //$total = $model->where($where)->count();
        $total = '';

        foreach ($list as &$item) {
            $orderId = Order::where('express_no', $item['express_no'])->value('id');
            $imgUrl = Orderitem::where('order_id', $orderId)->order('create_date desc')->value('product_img');
            $item['img_url'] = $imgUrl;
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }
}